package com.itheima.goods.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
/*import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;*/
import com.itheima.goods.mapper.GoodsMapper;
import com.itheima.goods.pojo.Goods;
import com.itheima.goods.pojo.Spec;
import com.itheima.goods.service.GoodsService;
import entity.PageResult;
import entity.QueryPageBean;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;


    @Override
    public Result findAll() {
        List<Goods> goodsList = null;
        try {
            goodsList = this.list();
            return new Result(true, StatusCode.OK, "查询成功",goodsList);
        } catch (Exception e) {
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }

    @Override
    public Result findPage(Integer page, Integer size, Object searchMap) {
        IPage<Goods> iPage = null;
        try {
            String searchMapString = JSON.toJSONString(searchMap);
            Goods goods = JSON.parseObject(searchMapString, Goods.class);

            iPage = new Page<>(page, size);
            page(iPage, new QueryWrapper<Goods>()
                    .orderByDesc("id")
                    .like(!StringUtils.isEmpty(goods.getName()),"name",goods.getName())
                    .like(!StringUtils.isEmpty(goods.getSn()),"sn",goods.getSn())
                    .like(!StringUtils.isEmpty(goods.getBrand()),"brand",goods.getBrand())
                    .like(!StringUtils.isEmpty(goods.getCaption()),"caption",goods.getCaption())
                    .eq(!StringUtils.isEmpty(goods.getPrice()),"price",goods.getPrice())
                    .eq(!StringUtils.isEmpty(goods.getNum()),"num",goods.getNum())
                    .like(!StringUtils.isEmpty(goods.getImage()),"image",goods.getImage())
                    .like(!StringUtils.isEmpty(goods.getImages()),"images",goods.getImages())
                    .like(!StringUtils.isEmpty(goods.getCategory()),"category",goods.getCategory())
                    .like(!StringUtils.isEmpty(goods.getSpec()),"spec",goods.getSpec())
                    .eq(!StringUtils.isEmpty(goods.getSaleNum()),"sale_num",goods.getSaleNum())
                    .like(!StringUtils.isEmpty(goods.getCommentNum()),"comment_num",goods.getCommentNum())
                    .like(!StringUtils.isEmpty(goods.getIsMarketable()),"is_marketable",goods.getIsMarketable())
                    .like(!StringUtils.isEmpty(goods.getCreatetime()),"createtime",goods.getCreatetime())
            );
           return new Result(true, StatusCode.OK, "查询成功", new PageResult<Goods>(iPage.getTotal(), iPage.getRecords()));

        } catch (Exception e) {

            return new Result(false, StatusCode.ERROR, "查询失败");
        }

    }


    @Override
    public Result findById(Integer id) {

        try {
            Goods goods = this.getBaseMapper().selectById(id);
            return new Result(true, StatusCode.OK, "查询成功",goods);
        } catch (Exception e) {
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }


    @Override
    public Result add(Goods goods) {
        boolean save = save(goods);
        if(save){
            return new Result(true, StatusCode.OK, "添加成功");
        }
        return new Result(false, StatusCode.ERROR, "添加失败");

    }

    @Override
    public Result updateGoods(Integer id, Goods goods) {
        boolean update = this.update(goods, new UpdateWrapper<Goods>().eq("id", id));
        if (update) {
            return new Result(true, StatusCode.OK, "更新成功");
        }

        return new Result(false, StatusCode.ERROR, "更新失败");
    }

    @Override
    public Result delete(Integer id) {
        boolean removeById = removeById(id);
        if (removeById) {
            return new Result(true, StatusCode.OK, "删除成功");
        }
        return new Result(false, StatusCode.ERROR, "删除失败");
    }

    @Override
    public Result upGoods(Integer id) {
        Goods goods = this.goodsMapper.selectById(id);
        if(!"1".equals(goods.getStatus())){
            Result result = new Result(false, StatusCode.ERROR, "上架失败");
            System.out.println(result+"当前商品处于异常状态");
        }
        goods.setIsMarketable("1");
        goods.setUpdatetime(new Date());
        this.goodsMapper.updateById(goods);
        return null;
    }

    @Override
    public Result downGoods(Integer id) {
        Goods goods = this.goodsMapper.selectById(id);
        if(!"1".equals(goods.getStatus())){
            Result result = new Result(false, StatusCode.ERROR, "下架失败");
            System.out.println(result+"当前商品已经处于下架状态");
        }
        goods.setIsMarketable("0");
        goods.setUpdatetime(new Date());
        this.goodsMapper.updateById(goods);
        return null;
    }

    @Override
    public Result updateStatus(Integer id, String status) {
        Goods goods = new Goods();
        goods.setId(id);
        goods.setStatus(status);
        goods.setUpdatetime(new Date());
        this.goodsMapper.updateById(goods);
        return null;
    }
}
